package com.tongji.housediy.view.titlewindows
{
	import com.adobe.protocols.dict.util.CompleteResponseEvent;
	import com.tongji.housediy.controller.CantavilCMD.GetCantavilDetailCmd;
	import com.tongji.housediy.events.CantavilEvent.GetAllCityEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilDetailEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilNameEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilResultEvent;
	import com.tongji.housediy.events.CantavilEvent.GetCantavilTypeEvent;
	import com.tongji.housediy.events.commonEvent.ShowComplementInforEvent;
	import com.tongji.housediy.events.houseEvent.OpenHouseEvent;
	import com.tongji.housediy.model.CantavilDropDownListModel;
	import com.tongji.housediy.model.CantavilModel;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.HouseModel;
	import com.tongji.housediy.view.CantavilViewStack;
	import com.tongji.housediy.vo.Cantavil;
	import com.tongji.housediy.vo.House;
	import com.tongji.housediy.vo.Type;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	public class CantavilTitleWindowMediator extends Mediator
	{
		[Inject]
		public var view:CantavilTitleWindow;
		
		[Inject]
		public var dropDownListModel:CantavilDropDownListModel;
		
		[Inject]
		public var cantavilModel:CantavilModel;
		
		[Inject]
		public var houseModel:HouseModel;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		private var currentPage:int = 1;
		private var currentHouse:House;
		private var canvialID:int = 0;
		override public function onRegister():void
		{
			canvialID = 0;
			currentPage = 1;
			view.currentState = "search";
			view.cantavilViewStack.selectedIndex = 0;
			
			BindingUtils.bindProperty(view.relatedList, "dataProvider", cantavilModel, "cantavilNameArrayCollection");
			BindingUtils.bindProperty(view.searchCantavilResult.dataGroup, "dataProvider", cantavilModel, "cantavilList");
			view.searchCantavilResult.addEventListener(IndexChangeEvent.CHANGE, searchCantavilHandler);
			cantavilModel.cantavilList = new ArrayCollection();
			
			//			BindingUtils.bindProperty(view.majorCitiesList, "dataProvider", 
			view.textInput.addEventListener(Event.ADDED, textChangeHandler);
			view.relatedList.addEventListener(IndexChangeEvent.CHANGE, listItemSelected);
			
			view.searchBtn.addEventListener(MouseEvent.MOUSE_DOWN, searchHandler);
			
			BindingUtils.bindProperty(view.cantavilViewStack.houseList.dataGroup, "dataProvider", houseModel, "houseArray");
			
			view.cantavilViewStack.houseList.addEventListener(IndexChangeEvent.CHANGE, chooseHouseHandler);
			view.cantavilViewStack.nextPage.addEventListener(MouseEvent.CLICK, nextPageHandler);
			view.cantavilViewStack.lastPage.addEventListener(MouseEvent.CLICK, previousPageHandler);
			view.closeBtn.addEventListener(MouseEvent.CLICK, closeBtnClickHandler);
			view.searchRadioBtn.addEventListener(MouseEvent.MOUSE_DOWN, changeToSearchStateHandler);
			view.chooseRadioBtn.addEventListener(MouseEvent.MOUSE_DOWN, changeToChooseStateHandler);
			view.addEventListener(GetCantavilTypeEvent.CHANGE_CITY, changeCity);	
			
			addContextListener(GetCantavilDetailEvent.UPDATE_CANTAVIL_DETAIL, updateCantavilHandler, GetCantavilDetailEvent);
		}
		
		private function updateCantavilHandler(event:GetCantavilDetailEvent):void
		{
			view.selectedCantavilList.selectedIndex = -1;
			BindingUtils.bindProperty(view.selectedCantavilList.dataGroup, "dataProvider", dropDownListModel, "cantavilDetailArrayCollection");

		}
		private function searchCantavilHandler(event:IndexChangeEvent):void
		{
			currentPage = 1;
			var canvial:Cantavil = view.searchCantavilResult.selectedItem as Cantavil;
			canvialID = canvial.cantavilID;
			dispatch(new OpenHouseEvent(OpenHouseEvent.GET_HOUSE_BY_CANTAVIL, 1, 6, canvial.cantavilID));	
		}
		
		private function selectCantavilHandler(event:IndexChangeEvent):void
		{
			currentPage = 1;
			var canvial:Cantavil = view.selectedCantavilList.selectedItem as Cantavil;
			canvialID = canvial.cantavilID;
			dispatch(new OpenHouseEvent(OpenHouseEvent.GET_HOUSE_BY_CANTAVIL, 1, 6, canvial.cantavilID));			
		}
		
		private function backBtnHandler(event:MouseEvent):void
		{
			view.cantavilViewStack.selectedIndex = 0; 
			view.cantavilViewStack.houseList.selectedItem = null;
		}
		
		private function searchHandler(event:MouseEvent):void
		{
			if (view.textInput.text.length > 0)
			{
				dispatch(new GetCantavilResultEvent(GetCantavilResultEvent.GET_CANTAVIL_BY_CANTAVIL_NAME, view.textInput.text, null));
			}
		}
		
		private function nextPageHandler(event:MouseEvent):void
		{
			if(canvialID == 0)
			{
				dispatch(new OpenHouseEvent(OpenHouseEvent.GAT_ALL_HOUSES, currentPage + 1, 6, 0));
			}
			else
			{
				dispatch(new OpenHouseEvent(OpenHouseEvent.GET_HOUSE_BY_CANTAVIL, currentPage + 1, 6, canvialID));			
			}
			
			currentPage = currentPage + 1;
		}
		
		private function previousPageHandler(event:MouseEvent):void
		{
			
			if(currentPage != 1)
			{
				if(canvialID == 0)
				{
					dispatch(new OpenHouseEvent(OpenHouseEvent.GAT_ALL_HOUSES, currentPage - 1, 6, 0));
				}
				else
				{
					dispatch(new OpenHouseEvent(OpenHouseEvent.GET_HOUSE_BY_CANTAVIL, currentPage - 1, 6, canvialID));			
				}				
				currentPage = currentPage - 1;
			}
		}
		
		private function chooseHouseHandler(event:IndexChangeEvent):void
		{
			currentHouse = new House;
			currentHouse = view.cantavilViewStack.houseList.selectedItem as House;
			view.cantavilViewStack.selectedIndex = 1;
			view.cantavilViewStack.housePic.source = currentHouse.housePicSrc;
			view.cantavilViewStack.houseName.text = currentHouse.houseName;
			//view.cantavilViewStack.belongArea.text = currentHouse.;
			view.cantavilViewStack.houseAddress.text = currentHouse.houseRemark;
			view.cantavilViewStack.houseArea.text = currentHouse.houseArea.toString();
			view.cantavilViewStack.backBtn.addEventListener(MouseEvent.CLICK, backBtnHandler);
			view.cantavilViewStack.okBtn.addEventListener(MouseEvent.CLICK, chooseSuccessHandler);
			//view.searchCantavilResult.removeEventListener(IndexChangeEvent.CHANGE, selectCantavilHandler);
			
		}
		
		private function chooseSuccessHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			roomModel.openHouse(new XML(currentHouse.houseModelSrc));
		}
		
		private function closeBtnClickHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			if(this.view.name != "haveStarted")
				dispatch(new ShowComplementInforEvent(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR));
		}
		
		private function changeToSearchStateHandler(event:MouseEvent):void
		{
			view.currentState = "search";
			view.relatedList.visible = false;
			cantavilModel.cantavilList = new ArrayCollection();
			cantavilModel.cantavilNameArrayCollection = new ArrayCollection();
			view.textInput.text = null;
		}
		
		private function changeToChooseStateHandler(event:MouseEvent):void
		{
			view.relatedList.visible = false;
			cantavilModel.cantavilList = new ArrayCollection();
			cantavilModel.cantavilNameArrayCollection = new ArrayCollection();
			view.textInput.text = null;
			view.currentState = "choose";
			
			BindingUtils.bindProperty(view.AGList.dataGroup, "dataProvider", cantavilModel, "AGCityArray");
			BindingUtils.bindProperty(view.HJList.dataGroup, "dataProvider", cantavilModel, "HJCityArray");
			BindingUtils.bindProperty(view.LSList.dataGroup, "dataProvider", cantavilModel, "LSCityArray");
			BindingUtils.bindProperty(view.TZList.dataGroup, "dataProvider", cantavilModel, "TZCityArray");

			view.selectedCantavilList.addEventListener(IndexChangeEvent.CHANGE, selectCantavilHandler);
			
		}
		
		private function textChangeHandler(event:Event):void
		{
			if (view.textInput.text.length > 0)
			{
				dispatch(new GetCantavilNameEvent(GetCantavilNameEvent.GET_CANTAVIL_NAME, view.textInput.text));
				eventMap.mapListener(eventDispatcher, GetCantavilNameEvent.UPDATE_CANTAVIL_MODEL, getCantavilNameHandler);
			}
			else if (view.textInput.text.length == 0)
			{
				cantavilModel.cantavilNameArrayCollection.removeAll();
				view.relatedList.visible = false;
			}
		}
		
		private function getCantavilNameHandler(event:Event):void
		{
			if (cantavilModel.cantavilNameArrayCollection.length > 0)
			{
				view.relatedList.visible = true;
			}
			else if (cantavilModel.cantavilNameArrayCollection.length == 0)
			{
				view.relatedList.visible = false;
			}
			eventMap.unmapListener(eventDispatcher, GetCantavilNameEvent.UPDATE_CANTAVIL_MODEL, getCantavilNameHandler);
		}
		
		private function listItemSelected(event:IndexChangeEvent):void
		{
			view.textInput.text = view.relatedList.selectedItem;
			view.relatedList.visible = false;
		}
		
		private function changeCity(event:GetCantavilTypeEvent):void
		{
			dropDownListModel.province.provinceName = event.target._CityDataListItemRenderer_Label1.text;
			dropDownListModel.city = event.cantavilType;
			
			BindingUtils.bindProperty(view.selectedProvinceText, "text", dropDownListModel.province, "provinceName");
			BindingUtils.bindProperty(view.selectedCityText, "text", dropDownListModel.city, "typeName");
			
			dispatch(new GetCantavilDetailEvent(GetCantavilDetailEvent.GET_CANTAVIL_DETAIL, dropDownListModel.city.typeID));
		}
	}
}